const db = uniCloud.database();
const jdb = uniCloud.databaseForJQL();
const {giveMsg,giveCode,result}=require('shop_common');

module.exports = {
	_before: function () { // 通用预处理器
		this.startTime = Date.now()
	},
	
	async get_index_swiper(){
		let res = await db.collection("familyHelper_list").where({"state":true}).orderBy("order_id","asc").get()
		return result(200,"success",res.data)
	},
	
	async get_index_navigation(){
		let res = await db.collection("familyHelper_categories").where({"is_index_show":true}).orderBy("order_id","asc").get()
		return result(200,"success",res.data)
	},
	
	async get_helperList_by_id(){
		let body = this.getHttpInfo().body
		if(!body) throw result(400,"required")
		this.params = JSON.parse(body)
		let {HelperList_id} = this.params
		if(!HelperList_id) throw result(400,"required")
		//let res = await db.collection("familyHelper_list").where({_id:HelperList_id}).get().doc(HelperList_id)
		let res = await db.collection("familyHelper_list").where({_id:HelperList_id}).get()
		return result(200,"success",res.data)
	},
	
	async get_First_HelperList(){
		let body = this.getHttpInfo().body
		if(!body) throw result(400,"required")
		this.params = JSON.parse(body)
		let {categories_id} = this.params
		if(!categories_id) throw result(400,"required")
		let res = await db.collection("familyHelper_list").where({parent_id:categories_id}).orderBy("order_id","asc").get()
		return result(200,"success",res.data)
	},
	
	async get_HelperList(){
		let res = await db.collection("familyHelper_list").orderBy("order_id","asc").get()
		return result(200,"success",res.data)
	},
	
	async add_requirement(){
		let body = this.getHttpInfo().body
		if(!body) throw result(400,"required")
		this.params = JSON.parse(body)
		
		let res = await db.collection("familyHelper_requirement").add(this.params)
		return result(201, "add", res.id);
	},
	
	async get_requirement(){
		let body = this.getHttpInfo().body
		if(!body) throw result(400,"required")
		this.params = JSON.parse(body)
		let {pageNo, pageSize, id} = this.params
		let skip = (pageNo - 1) * pageSize
		let res = await db.collection("familyHelper_requirement")
		/* .where({"parent_id":id}) */
		.orderBy("createTime",'desc')
		.skip(skip)
		.limit(pageSize)
		.get()
		return result(200,"success",res.data)
	},
	
	_after(error,result){
		if(error){
			throw error
		}
		result.timeCost = Date.now() - this.startTime
		return result
	}
}
